<template>
    <div>
        <h1>登录</h1>
        <table class="table" border="1">        
        <tr>
            <td>用户名</td>
            <td><input type="text" v-model="U_Lname"></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" v-model="U_Lpass"></td>
        </tr>
        <tr>
            <td colspan="2"><button @click="login">登录</button></td>
        </tr>
        </table>

    </div>
</template>
<script setup lang="ts">

import { ref } from 'vue';
import axios from 'axios';
const U_Lname = ref('');
const U_Lpass = ref('');

import { useRouter } from 'vue-router';

const router = useRouter();


const login=()=>{
    if(U_Lname.value=='')
    {
        alert('用户名不能为空');
        return;
    }
    //密码不能为空并需要包含字母、数字、特殊字符
    if(U_Lpass.value=='')
    {
        alert('密码不能为空');
        return;
    }
    if(!/^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[^a-zA-Z0-9]).{8,16}$/.test(U_Lpass.value))
    {
        alert('密码必须包含字母、数字、特殊字符');
        return;
    }

    axios({
        method:'get',
        url:'http://localhost:62255/api/Two/UserLogin',
        params:{
            name:U_Lname.value,
            pass:U_Lpass.value
        }
    }).then(res=>{
        if(res.data>0){
            alert('登录成功');
            router.push('/LoginInfo');
        }
        else if(res.data==-1)
        {
            alert('用户名不存在');
        }
        else{
            alert('登录失败');
        }
    })
    .catch(err=>{
        console.log(err)
    })
}



</script>
